<?php

namespace Lib\Behavior;

use Think\Behavior,
    Common\Common\Common,
    Common\Common\ReturnParams;

class OutputBehavior extends Behavior
{
    // 行为参数定义
    protected $options   =  array(
    );

    // 行为扩展的执行入口
    public function run(&$params)
    {
        $result = json_encode(array(
                'REQUESTID' => Common::uuid(),
                'ACTION'    => $_REQUEST['api_action'],
                'CODE'      => ReturnParams::$code,
                'DATA'      => ReturnParams::$data,
            )
        );

        if ($_REQUEST['api_client_id'])
        {
            echo $result;
            return;
        }

        if (C('ENVIRONMENT') == 'development')
        {
            $result = Common::json_indent($result);
            $result = "<pre>".$result."</pre>";
        }

        if ($_REQUEST['api_format'] == 'jsonp' && $_REQUEST['api_action'] != 'batch')
        {
            header("Content-Type", "application/x-javascript");
            //返回jsonp数据格式
            $api_callback = $_REQUEST['api_callback'] ? '_callback' : $_REQUEST['api_callback'];
            $result = $api_callback.'('.$result.');';
        }
        else
        {
            header("Content-Type", "application/json");
        }

        echo($result);
    }

}
